package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Update extends JInternalFrame {
	Connection conn;
	DatabaseTest dt;
	int s;//검색중인 시리얼 번호
	private JLabel label;
	private JLabel label_1;
	private JTextField code;
	private JTextField ea;
	private JButton button;
	private JLabel status;
	private JButton button_1;
	private JTextField serial;
	private JLabel lblSerial;

	/**
	 * Create the frame.
	 */
	public Update(Connection c, DatabaseTest d) {
		super("수정", true, true, true, false);
		this.conn = c;
		this.dt = d;
		setBounds(100, 100, 800, 100);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getButton());
		getContentPane().add(getStatus());
		getContentPane().add(getButton_1());
		getContentPane().add(getSerial());
		getContentPane().add(getLblSerial());

	}

	public void update() {

		try {
			String c = code.getText();
			int e = Integer.parseInt(ea.getText());

			String sql = "update ptbl set code = ?, ea = ? where serial = ?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, c);  
			pstmt.setInt(2, e);
			pstmt.setInt(3, s);

			pstmt.executeUpdate();
			System.out.println("수정!");
			pstmt.close();

			if (dt.select != null) {
				dt.select.setList(dt.select.sql.getText());
			}

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}

	}

	public void setData(String s, String c, String e) {
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드");
			label.setHorizontalAlignment(SwingConstants.RIGHT);
			label.setBounds(12, 43, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량");
			label_1.setHorizontalAlignment(SwingConstants.RIGHT);
			label_1.setBounds(286, 43, 57, 15);
		}
		return label_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(81, 40, 193, 21);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(355, 40, 193, 21);
		}
		return ea;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					update();
				}
			});
			button.setBounds(560, 40, 212, 21);
		}
		return button;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel(" ");
			status.setHorizontalAlignment(SwingConstants.CENTER);
			status.setBounds(22, 13, 252, 15);
		}
		return status;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("검색");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial = ?";
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);//첫번째 물음표에 s
						
						ResultSet rs = pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
						}else{
							status.setText("데이터가 없습니다.");
							code.setText("");
							ea.setText("");
						}
						rs.close();pstmt.close();
					} catch (Exception e) {
						// TODO: handle exception
						System.out.println(e.getMessage());
					}
				}
			});
			button_1.setBounds(560, 10, 212, 21);
		}
		return button_1;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(355, 10, 193, 21);
		}
		return serial;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("Serial");
			lblSerial.setHorizontalAlignment(SwingConstants.RIGHT);
			lblSerial.setBounds(286, 13, 57, 15);
		}
		return lblSerial;
	}
}
